﻿using System;
using System.Collections.Specialized;
using System.Text;

namespace MediaStreamer
{
    public class RequestInfo
    {
        public string ResourceId { get; set; }
        public NameValueCollection Query { get; set; }
        public string UserAgent { get; set; }
    }

    public static class QueryExtensions
    {
        public static string AsString(this NameValueCollection nameValueCollection)
        {
            StringBuilder sb = new StringBuilder();
            foreach (var key in nameValueCollection.AllKeys)
            {
                sb.Append(sb.Length == 0 ? "?" : "&");
                sb.Append(key);
                sb.Append("=");

                sb.Append(nameValueCollection.Get(key));
            }

            return sb.ToString();
        }

        public static NameValueCollection UpdateWithCollection(this NameValueCollection nameValueCollection, NameValueCollection appendNameValueCollection)
        {
            var target = new NameValueCollection(nameValueCollection);
            foreach (var key in appendNameValueCollection.AllKeys)
            {
                target.Set(key, appendNameValueCollection.Get(key));
            }

            return target;
        }

        public static bool IsEqual(this RequestInfo requestInfo, string url)
        {
            if (requestInfo == null)
                return false;

            var requestUrl = requestInfo.ResourceId + requestInfo.Query.AsString();

            return string.Equals(requestUrl, url);
        }
    }
}
